<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>UIImage | XMG</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../avplayer/README.html" />
    
    
    <link rel="prev" href="../nsobject/README.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="1.1" data-basepath=".." data-revision="1456708040086">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Introduction
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="nsobject/README.html">
            
                
                    <a href="../nsobject/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         NSObject
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter active" data-level="1.1" data-path="uiimage/README.html">
            
                
                    <a href="../uiimage/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                         UIImage
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.2" data-path="avplayer/README.html">
            
                
                    <a href="../avplayer/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                         AVPlayer
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.3" data-path="nsobject/uiresponder.html">
            
                
                    <a href="../nsobject/uiresponder.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                         UIResponder
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="1.3.1" data-path="ui/uiview.html">
            
                
                    <a href="../ui/uiview.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.1.</b>
                        
                         UIView
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.3.2" data-path="uiviewcontroller/README.html">
            
                
                    <a href="../uiviewcontroller/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.2.</b>
                        
                         UIViewController
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >XMG</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_38">
                    
                        <h1 id="uiimage">UIImage</h1>
<ul>
<li>继承自NSObject</li>
</ul>
<h2 id="contentmode">contentMode属性</h2>
<ul>
<li><p>带有scale单词的：图片有可能会拉伸</p>
<ul>
<li>UIViewContentModeScaleToFill<ul>
<li>将图片拉伸至填充整个imageView</li>
<li>图片显示的尺寸跟imageView的尺寸是一样的</li>
</ul>
</li>
<li>带有aspect单词的：保持图片原来的宽高比<ul>
<li>UIViewContentModeScaleAspectFit<ul>
<li>保证刚好能看到图片的全部</li>
</ul>
</li>
<li>UIViewContentModeScaleAspectFill<ul>
<li>拉伸至图片的宽度或者高度跟imageView一样</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><p>没有scale单词的：图片绝对不会被拉伸，保持图片的原尺寸</p>
<ul>
<li>UIViewContentModeCenter</li>
<li>UIViewContentModeTop</li>
<li>UIViewContentModeBottom</li>
<li>UIViewContentModeLeft</li>
<li>UIViewContentModeRight</li>
<li>UIViewContentModeTopLeft</li>
<li>UIViewContentModeTopRight</li>
<li>UIViewContentModeBottomLeft</li>
<li>UIViewContentModeBottomRight</li>
</ul>
</li>
</ul>
<h2 id="">小语法点</h2>
<ul>
<li>不能直接修改：OC对象的结构体属性的成员</li>
<li>下面的写法是错误的</li>
</ul>
<pre><code class="lang-objc">imageView<span class="hljs-variable">.frame</span><span class="hljs-variable">.size</span> = imageView<span class="hljs-variable">.image</span><span class="hljs-variable">.size</span>;
</code></pre>
<ul>
<li>正确写法</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-built_in">CGRect</span> tempFrame = imageView<span class="hljs-variable">.frame</span>;
tempFrame<span class="hljs-variable">.size</span> = imageView<span class="hljs-variable">.image</span><span class="hljs-variable">.size</span>;
imageView<span class="hljs-variable">.frame</span> = tempFrame;
</code></pre>
<h2 id="initwithimage">initWithImage:方法</h2>
<ul>
<li>利用这个方法创建出来的imageView的尺寸和传入的图片尺寸一样</li>
</ul>
<h2 id="frame3">修改frame的3种方式</h2>
<ul>
<li>直接使用CGRectMake函数</li>
</ul>
<pre><code class="lang-objc">imageView<span class="hljs-variable">.frame</span> = CGRectMake(<span class="hljs-number">100</span>, <span class="hljs-number">100</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>);
</code></pre>
<ul>
<li>利用临时结构体变量</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-built_in">CGRect</span> tempFrame = imageView<span class="hljs-variable">.frame</span>;
tempFrame<span class="hljs-variable">.origin</span><span class="hljs-variable">.x</span> = <span class="hljs-number">100</span>;
tempFrame<span class="hljs-variable">.origin</span><span class="hljs-variable">.y</span> = <span class="hljs-number">100</span>;
tempFrame<span class="hljs-variable">.size</span><span class="hljs-variable">.width</span> = <span class="hljs-number">200</span>;
tempFrame<span class="hljs-variable">.size</span><span class="hljs-variable">.height</span> = <span class="hljs-number">200</span>;
imageView<span class="hljs-variable">.frame</span> = tempFrame;
</code></pre>
<ul>
<li>使用大括号{}形式</li>
</ul>
<pre><code class="lang-objc">imageView<span class="hljs-variable">.frame</span> = (<span class="hljs-built_in">CGRect</span>){{<span class="hljs-number">100</span>, <span class="hljs-number">100</span>}, {<span class="hljs-number">200</span>, <span class="hljs-number">200</span>}};
</code></pre>
<ul>
<li><p>抽取重复代码</p>
<ul>
<li>将相同代码放到一个新的方法中</li>
<li>不用的东西就变成方法的参数</li>
</ul>
</li>
<li><p>图片的加载方式</p>
<ul>
<li>有缓存<pre><code class="lang-objc"><span class="hljs-built_in">UIImage</span> *image = [<span class="hljs-built_in">UIImage</span> imageNamed:<span class="hljs-string">@"图片名"</span>];
</code></pre>
<ul>
<li>使用场合：图片比较小、使用频率较高</li>
<li>建议把需要缓存的图片直接放到Images.xcassets</li>
</ul>
</li>
<li>无缓存<pre><code class="lang-objc"><span class="hljs-built_in">NSString</span> *file = [[<span class="hljs-built_in">NSBundle</span> mainBundle] pathForResource:<span class="hljs-string">@"图片名"</span> ofType:<span class="hljs-string">@"图片的扩展名"</span>];
<span class="hljs-built_in">UIImage</span> *image = [<span class="hljs-built_in">UIImage</span> imageWithContentsOfFile:<span class="hljs-string">@"图片文件的全路径"</span>];
</code></pre>
<ul>
<li>使用场合：图片比较大、使用频率较小</li>
<li>不需要缓存的图片不能放在Images.xcassets</li>
</ul>
</li>
<li>放在Images.xcassets里面的图片，只能通过图片名去加载图片</li>
</ul>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../nsobject/README.html" class="navigation navigation-prev " aria-label="Previous page: NSObject"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../avplayer/README.html" class="navigation navigation-next " aria-label="Next page: AVPlayer"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
